﻿//********************************************************************
// 
//  file:  frmUserContracts.cs
// 
//  (C) Copyright 2010, Diomede Corporation
//  All rights reserved
// 
//  Use, modification, and distribution is subject to   
//  the New BSD License (See accompanying file license.txt).
// 
//********************************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Diomede.SDK;
using data.diomedestorage.com;

namespace Diomede.Explorer
{
    public partial class frmUserContracts : Form
    {
        public frmUserContracts()
        {
            InitializeComponent();
        }

        public frmUserContracts(ISession session)
        {
            InitializeComponent();
            Session = session;
        }

        private void frmUserContracts_Load(object sender, EventArgs e)
        {
            loadUserContracts();
        }

        public ISession Session { get; set; }

        void loadUserContracts()
        {
            IPurchasingManagement p = Session as IPurchasingManagement;
            UsersContractsExecResult result = p.GetMyContracts();
            if (result.Status == Diomede.SDK.Enums.ExecutionResults.Success)
            {
                listBox.DataSource = result.UserContracts;
            }
            else
            {
                MessageBox.Show(this, result.Message, "Error");
            }
        }

        private void btnPurchaseContract_Click(object sender, EventArgs e)
        {
            frmAllContracts frm = new frmAllContracts(Session);
            frm.StartPosition = FormStartPosition.CenterParent;
            if (frm.ShowDialog(this) == DialogResult.OK)
            {
                if (frm.SelectedContract.Count() > 0)
                {
                    IPurchasingManagement p = Session as IPurchasingManagement;
                    foreach (var item in frm.SelectedContract)
                    {
                        ContractsExecResult purchaseResult = p.PurchaseContract(item.contractID);
                        if (purchaseResult.Status != Diomede.SDK.Enums.ExecutionResults.Success)
                        {
                            MessageBox.Show(this, purchaseResult.Message, "Error");
                        }
                    }
                    loadUserContracts();
                }
                
            }
        }

        private void listBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox.SelectedIndices.Count > 0)
            {
                btnCancelContract.Enabled = true;
            }
            else
            {
                btnCancelContract.Enabled = false;
            }
        }

        private void btnCancelContract_Click(object sender, EventArgs e)
        {
            if (listBox.SelectedItems.Count > 0)
            {
                IPurchasingManagement p = Session as IPurchasingManagement;
                foreach (var item in listBox.SelectedItems)
                {
                    UserContract c = item as UserContract;
                    ContractsExecResult cancelResult = p.CancelContract(c.userContractID);
                    if (cancelResult.Status != Diomede.SDK.Enums.ExecutionResults.Success)
                    {
                        MessageBox.Show(this, cancelResult.Message, "Error");
                    }
                }
                loadUserContracts();
            }
        }
    }
}
